﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Data;
using T.ServiceComponent.DAC.Service;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.Base.Dao;

namespace T.FAS.Runtime.DataModel.Dao
{
    public class DataQuerySourceELBDao : DaoService<DataQuerySource>
    {
        public override string TableName => "FAS_DataQuerySource";
        public override string IDColumnName => "ID";
        public string ForeignColumn => "DataQueryID";
        public override List<string> ExceptColumnName {get;} = DaoUtil.GetExceptionColumn<DataQuerySource>();

        public override Dictionary<string, object> GetDataMapping(DataQuerySource data)
        {
            var datas = base.GetDataMapping(data);
            datas.Remove(nameof(DataQuerySource.SourceType));
            datas.Add(nameof(DataQuerySource.SourceType), (int)data.SourceType);
            return datas;
        }

        public override DataQuerySource ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            if (columns.ContainsKey(nameof(DataQuerySource.SourceType)))
                columns.Remove(nameof(DataQuerySource.SourceType));

            var data = base.ConvertToObject(columns, row);

            string sourceTypeStr = row[nameof(DataQuerySource.SourceType)]?.ToString();
            if (!string.IsNullOrEmpty(sourceTypeStr))
            {
                if (Enum.TryParse(sourceTypeStr, out DataQuerySourceType sourceType))
                {
                    data.SourceType = sourceType;
                }
                else
                    data.SourceType = DataQuerySourceType.DataObject;
            }
            else
                data.SourceType = DataQuerySourceType.DataObject;
            return data;
        }

        public DataQuerySourceELBDao() : base()
        {
            DBName = "TFAS";
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="dqId"></param>
        /// <returns></returns>
        public List<DataQuerySource> GetDataQuerySources(string dqId)
        {
            var result = new List<DataQuerySource>();
            if (string.IsNullOrEmpty(dqId))
            {
                return result;
            }
            List<object> objects = base.GetObjectsByFilter($"{ForeignColumn}='{dqId}'");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as DataQuerySource));
            }
            return result;
        }

        /// <summary>
        /// 添加DataQuerySource
        /// </summary>
        /// <param name="dqSources"></param>
        public void CreateDataQuerySource(List<DataQuerySource> dqSources)
        {
            try
            {
                base.Add(dqSources);
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DQ.CreateSourceError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除DataQuerySource
        /// </summary>
        /// <param name="dqId"></param>
        public void DeleteDataQuerySource(string dqId)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, dqId));
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DQ.DeleteSourceError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除DataQuerySource
        /// </summary>
        /// <param name="dqIds"></param>
        public void BatchDeleteDataQuerySource(List<string> dqIds)
        {
            try
            {
                List<string> sqls = new List<string>();
                foreach (string dqId in dqIds)
                {
                    sqls.Add(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, dqId));
                }
                base.DB.ExecSqlStatement(sqls.ToArray());
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DQ.DeleteSourceError, e.Message, e);
            }
        }
    }
}